package com.tsmti.system.dao.impl;

import com.tsmti.core.common.dao.impl.BaseDaoImpl;
import com.tsmti.core.util.BeanTransformerAdapter;
import com.tsmti.system.dao.SystemPageColumnDao;
import com.tsmti.system.dto.PageColDto;
import org.hibernate.Query;
import org.springframework.stereotype.Repository;
import java.util.List;
/**
 * @Package：com.tsmti.system.dao.impl
 * @Class：SystemPageColumnDaoImpl
 * @Description： TODO
 * @Author：zcw
 * @Date：Created in 2018/5/29 12:12
 * @Company:
 * @Version：
 * @Modified By:
 */
@Repository("systemPageColumnDao")
public class SystemPageColumnDaoImpl extends BaseDaoImpl implements SystemPageColumnDao{

    @Override
    public List<PageColDto> getUserDefaultColList(String pageId, String userName,boolean onlyUser) {
        StringBuilder sql = new StringBuilder();
        sql.append("select spc.id,spc.pageid,spc.colname,spc.coltext,upc.colWidth,upc.orders from pims_user_pagecol upc left join  pims_system_pagecol spc on upc.pageId=spc.pageId");
        sql.append(" where upc.colId = spc.id");
        sql.append(" and ( upc.userName =:userName and spc.pageId =:pageId)");
        sql.append(" order by upc.orders,spc.orders");

        Query query = getSession().createSQLQuery(sql.toString()).setResultTransformer((new BeanTransformerAdapter(PageColDto.class)));
        query.setString("userName", userName);
        query.setString("pageId", pageId);
        List<PageColDto> pageColDtoList =  query.list();
        boolean existed = (!onlyUser && (pageColDtoList ==null || pageColDtoList.size() <= 0));
        if(existed){
            pageColDtoList =  getSystemDefaultColList(pageId);
        }
        return  pageColDtoList;
    }

    /**
     * 系统默认显示列表
     * @param pageId
     * @return
     */
    @Override
    public List<PageColDto> getSystemDefaultColList(String pageId) {
        StringBuilder sql = new StringBuilder();
        sql.append("select spc.id,spc.pageid,spc.colname,spc.coltext,spc.colWidth,spc.orders from  pims_system_pagecol spc where spc.pageId=:pageId");
        Query query = getSession().createSQLQuery(sql.toString()).setResultTransformer((new BeanTransformerAdapter(PageColDto.class)));
        query.setString("pageId", pageId);
        List<PageColDto> pageColDtoList =  query.list();
        return  pageColDtoList;
    }
}
